package com.smec.remes.module.search.component;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.WindowManager;

import com.hwangjr.rxbus.Bus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.smec.moblie.R;
import com.smec.moblie.databinding.DialogBottomBinding;
import com.smec.remes.bean.AbleMatnr;
import com.smec.remes.module.search.adapter.SmecBuyAdapter;
import com.smec.remes.util.CommonUtils;

import java.util.ArrayList;
import java.util.HashMap;

import static android.view.Gravity.BOTTOM;
import static com.smec.remes.module.search.SmecSearchActivity.BUY_NOT_NULL;
import static com.smec.remes.module.search.SmecSearchActivity.BUY_NULL;


/**
 * Created by cjy on 2018/1/23.
 */

public class BottomDialog extends Dialog {
    public static final String BUY_LIST_NULL = "_buyListNull";

    private DialogBottomBinding dialogBottomBinding;
    private Context mContext;
    private ArrayList<AbleMatnr> arrayList=new ArrayList<>();
    private SmecBuyAdapter adapter;
    private Bus mRxbus;
    private int offSetY;

    public BottomDialog(@NonNull Context context, @StyleRes int themeResId, Bus rxBus) {
        super(context, themeResId);
        mContext = context;
        mRxbus = rxBus;
        dialogBottomBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.dialog_bottom,null,false);
        adapter = new SmecBuyAdapter(mContext,mRxbus);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(dialogBottomBinding.getRoot());
        _initView();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        arrayList = adapter._getList();
        if(CommonUtils.notEmpty(arrayList)){
            if(arrayList.size() == 0){
                mRxbus.post(BUY_NULL,new Object());
            }else {
                ArrayList<AbleMatnr> nelist=new ArrayList<>();

                for(AbleMatnr ableMatnr:arrayList){
                    if(ableMatnr.getNum()!=null && !ableMatnr.getNum().equals("") && !ableMatnr.getNum().equals("0")) {
                        nelist.add(ableMatnr);
                    }
                }

                if(CommonUtils.notEmpty(nelist)){
                    for(AbleMatnr ableMatnr:nelist){
                        String a[] = ableMatnr.getMatnrPrice()==null ?
                                new String[]{} : ableMatnr.getMatnrPrice().split(" ");
                        try {
                            if(a[0]!=null){
                                String price=a[0].trim();
                                Double i=Double.valueOf(price)*Integer.valueOf(ableMatnr.getNum());
                                if(a[2]!=null){
                                    i=i/Double.valueOf(a[3]);
                                }
                                if(!i.isNaN()){
                                    ableMatnr.setPriceALL(String.format("%.2f",i));
                                }else {
                                    ableMatnr.setPriceALL("0");
                                }

                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                    HashMap<String,Object> hashMap = new HashMap<>();
                    hashMap.put("size",nelist.size()+ "");
                    hashMap.put("newList",nelist);
                    mRxbus.post(BUY_NOT_NULL,hashMap);
                }else {
                    mRxbus.post(BUY_NULL,new Object());
                }


            }
        }else {
            mRxbus.post(BUY_NULL,new Object());
        }
    }

    private void _initView(){
        this.getWindow().setGravity(BOTTOM);
        this.getWindow().setWindowAnimations(R.style.DialogAnimation);
        WindowManager.LayoutParams lp = this.getWindow().getAttributes(); // 获取对话框当前的参数值
        lp.x = 0; // 新位置X坐标
        lp.y = 0; // 新位置Y坐标
        lp.width = (int) mContext.getResources().getDisplayMetrics().widthPixels; // 宽度
        dialogBottomBinding.getRoot().measure(0, 0);
        //lp.height = dialogBottomBinding.getRoot().getMeasuredHeight();

        lp.alpha = 9f; // 透明度
        this.getWindow().setAttributes(lp);

        dialogBottomBinding.list.setLayoutManager(new LinearLayoutManager(mContext));
        dialogBottomBinding.list.setAdapter(adapter);
    }

    public ArrayList<AbleMatnr> getArrayList() {
        return arrayList;
    }

    public void setArrayList(ArrayList<AbleMatnr> arrayList) {
        if(CommonUtils.notEmpty(arrayList)) {
            this.arrayList = arrayList;
            this.adapter.setArrayList(this.arrayList);
            this.adapter.notifyDataSetChanged();
        }
    }

    public int getOffSetY() {
        return offSetY;
    }

    public void setOffSetY(int offSetY) {
        this.offSetY = offSetY;
    }


    /**
     * 当购买列表长度为0时
     */
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(BUY_LIST_NULL)
            }
    )
    public void _buyListNull(Object a){
        arrayList = adapter._getList();
        dismiss();
    }
}
